package cn.com.self.apis.member.service.impl;

import cn.com.self.apis.core.vo.ResultModel;
import cn.com.self.apis.member.entity.vo.MemberInfoVO;
import cn.com.self.apis.member.entity.UMemberInfo;
import cn.com.self.apis.member.entity.UMemberInfoRelation;
import cn.com.self.apis.member.entity.vo.UMemberInfoVO;
import cn.com.self.apis.member.mapper.MemberInfoMapper;
import cn.com.self.apis.member.mapper.MemberInfoRelationMapper;
import cn.com.self.apis.member.service.IMemberInfoRelationService;
import cn.com.self.apis.member.service.IMemberInfoService;
import cn.com.self.apis.portal.entity.dto.RequirementsDTO2;
import cn.com.self.apis.portal.util.*;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import cn.com.selfsoft.smartcity.core.service.BaseServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class MemberInfoServiceImpl extends BaseServiceImpl<UMemberInfo, String> implements IMemberInfoService {

    @Autowired
    private MemberInfoRelationMapper memberInfoRelationMapper;
    @Autowired
    private MemberInfoMapper memberInfoMapper;

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private IMemberInfoRelationService memberRelationService;

    @Override
    public ResponseEntity deleteInfo(String id) {
        UMemberInfo load = this.load(id);
        if(load == null){
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(404,"不存在的账号信息！"));
        }
        UMemberInfo uMemberInfo = new UMemberInfo();
        uMemberInfo.setDelFlag("1");
        CopyUtils.copyProperties(uMemberInfo, load);
        this.update(load);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }

    @Override
    public ICustomPage<UMemberInfoVO> queryMemberInfoToTechnology(Map<String,Object> queryParams, Pageable pageable) {
        CustomPage<UMemberInfoVO> page = new CustomPage<>(pageable.getPageNumber(),pageable.getPageSize());
        ICustomPage<UMemberInfoVO> result=memberInfoMapper.queryMemberInfoToTechnology(page,queryParams);
        if(result !=null && result.getRows() !=null){
            setDataType(result.getRows());
        }
        return result;

    }

    @Override
    public UMemberInfoVO queryMemberInfobyMemberId(Map<String, Object> queryParams) {
        UMemberInfoVO uMemberInfoVO=memberInfoMapper.queryMemberInfobyMemberId(queryParams);
        if(uMemberInfoVO != null){
            setData(uMemberInfoVO);
            List<UMemberInfoRelation> list = memberRelationService.queryByMemberInfoId(""+uMemberInfoVO.getId());
            uMemberInfoVO.setMemberInfoRelations(list);
        }
        return uMemberInfoVO;
    }

    /**
     * 字典回显
     */
    public void setDataType(List<UMemberInfoVO> list){
        if(list!=null && list.size()>0){
            for(UMemberInfoVO mv:list){
                setData(mv);
            }
        }
    }

    public void setData(UMemberInfoVO mv){
        Object memberType_val= redisUtils.get(RedisCodeUtil.datatype_name+RedisCodeUtil.memberType+mv.getMemberType());
        Object realState_val= redisUtils.get(RedisCodeUtil.datatype_name+RedisCodeUtil.realState+mv.getRealState());
        Object level_val= redisUtils.get(RedisCodeUtil.datatype_name+RedisCodeUtil.level+mv.getLevel());
        Object province_val= redisUtils.get(RedisCodeUtil.datatype_name+RedisCodeUtil.province+mv.getProvince());
        Object onSite_val= redisUtils.get(RedisCodeUtil.datatype_name+RedisCodeUtil.on_site+mv.getOnSite());
        mv.setMemberType_val(memberType_val);
        mv.setRealState_val(realState_val);
        mv.setLevel_val(level_val);
        mv.setProvince_val(province_val);
        mv.setOnSite_val(onSite_val);
    }


    @Override
    public ResponseEntity getMemberInfoPage(HttpServletRequest request, Pageable pageable) {
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        //queryParams.put("del_flag",0);
        //queryParams.put("del_flag",0);
        //Page<UMemberInfo> pageData = this.page(queryParams, page);
        //return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataPage(pageData)));

        CustomPage<MemberInfoVO> page = new CustomPage<>(pageable.getPageNumber(),pageable.getPageSize());
        //IPage<MemberInfoVO> iPage = this.memberInfoMapper.getMemberInfoPage(page,queryParams.get("real_name"),queryParams.get("name"));
        ICustomPage<MemberInfoVO> iPage = this.memberInfoMapper.getMemberInfoPage(page,queryParams);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(iPage)));
    }

    @Override
    public ResponseEntity loadMemberInfo(String id) {
        UMemberInfo load = this.load(id);
        if(load == null){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResultModel<>(404,"不存在的账号信息！"));
        }
        MemberInfoVO memberInfoVO = new MemberInfoVO();
        CopyUtils.copyProperties(load, memberInfoVO);
        List<UMemberInfoRelation> list = memberRelationService.queryByMemberInfoId(id);
        memberInfoVO.setMemberInfoRelations(list);
        load.setShowNum(load.getShowNum().add(new BigDecimal(1)));
        this.update(load);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(memberInfoVO)));
    }

//    @Override
//    public ResponseEntity getMemberInfoPageByPortal(HttpServletRequest request, Pageable pageable) {
//        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
//
//        Page<MemberInfoVO> page = new Page<>(pageable.getPageNumber(),pageable.getPageSize());
//        IPage<MemberInfoVO> iPage = this.memberInfoMapper.getMemberInfoPageByPortal(page,queryParams);
//        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(iPage)));
//    }
}
